#include "File.h"

bool File::OpenRead(const string& filename)
{
	assert(!m_file.is_open());

	m_file.open(filename);

	assert(m_file.is_open());

	return (m_file.is_open());
}

bool File::OpenRead(const wstring& filename)
{
	assert(!m_file.is_open());

	m_file.open(filename);

	assert(m_file.is_open());

	return (m_file.is_open());
}

bool File::GetInteger(int* result)
{
	string str;
	if (!GetString(&str))
	{
		return false;
	}

	*result = ToInt(str);
	
	return true;
}

bool File::GetFloat(float* result)
{
	string str;
	if (!GetString(&str))
	{
		return false;
	}

	*result = ToFloat(str);

	return true;
}

bool File::GetD3DXVECTOR3(D3DXVECTOR3* result)
{
	string str;
	if (!GetString(&str))
	{
		return false;
	}

	Strings s;
	s = Split(str, ',');
	assert(s.size() == 3);

	result->x = ToFloat(s[0]);
	result->y = ToFloat(s[1]);
	result->z = ToFloat(s[2]);

	return true;
}

bool File::GetD3DXVECTOR2(D3DXVECTOR2* result)
{
	string str;
	if (!GetString(&str))
	{
		return false;
	}

	Strings s;
	s = Split(str, ',');
	assert(s.size() == 2);

	result->x = ToFloat(s[0]);
	result->y = ToFloat(s[1]);

	return true;
}

bool File::GetVectorI(std::vector<int>* result)
{
	std::string str;
	if (!GetString(&str))
	{
		return false;
	}

	Strings s;
	s = Split(str, ',');
	
	for (int i = 0;i != s.size(); i++)
	{
		result->push_back(ToInt(s[i]));
	}
	return true;
}

bool File::GetString(string* result)
{
	m_blockComment = false;

	while(m_file.good())
	{
		getline(m_file, *result);

		if (m_file.eof())
		{
			return false;
		}

		if ((result->substr(0, 2) == "/*"))
		{
			m_blockComment = true;
			continue;
		}

		if (m_blockComment == true)
		{
			if ((result->substr(0, 2) == "*/"))
			{
				m_blockComment = false;
				continue;
			}
		}

		if (m_blockComment == false)
		{
			if (result->empty())
			{
				continue;
			}

			if ((result->substr(0, 2) == "//"))
			{
				continue;
			}

			if ((result->substr(0, 10) == "IMACOMMENT"))
			{
				continue;
			}

			if ((*result)[0] == '#')
			{
				continue;
			}
			break;
		}
	}
	return true;
}

bool File::EndOfFile()
{
	return m_file.eof();
}

bool File::CloseFile(const string& filename)
{
	assert(m_file.is_open());
	if (m_file.is_open())
	{
		m_file.close();
		return true;
	}

	return false;
}